package org.ruoyi.dhuman.service.impl;

import org.ruoyi.common.core.utils.MapstructUtils;
    import org.ruoyi.core.page.TableDataInfo;
    import org.ruoyi.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.ruoyi.dhuman.domain.bo.DhumanPptListBo;
import org.ruoyi.dhuman.domain.vo.DhumanPptListVo;
import org.ruoyi.dhuman.domain.DhumanPptList;
import org.ruoyi.dhuman.mapper.DhumanPptListMapper;
import org.ruoyi.dhuman.service.IDhumanPptListService;
import org.ruoyi.common.core.utils.StringUtils;

import java.util.List;
import java.util.Collection;

/**
 * 数字人讲PPT子表Service业务层处理
 *
 * @author wangliang
 * @date Tue Oct 14 12:06:18 CST 2025
 */
@RequiredArgsConstructor
@Service
public class DhumanPptListServiceImpl implements IDhumanPptListService {

    private final DhumanPptListMapper baseMapper;

    /**
     * 查询数字人讲PPT子表
     */
    @Override
    public DhumanPptListVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

	/**
	 * 查询数字人讲PPT子表列表
	 */
	@Override
	public TableDataInfo<DhumanPptListVo> queryPageList(DhumanPptListBo bo, PageQuery pageQuery) {
		LambdaQueryWrapper<DhumanPptList> lqw = buildQueryWrapper(bo);
		Page<DhumanPptListVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

    /**
     * 查询数字人讲PPT子表列表
     */
    @Override
    public List<DhumanPptListVo> queryList(DhumanPptListBo bo) {
        LambdaQueryWrapper<DhumanPptList> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DhumanPptList> buildQueryWrapper(DhumanPptListBo bo) {
        LambdaQueryWrapper<DhumanPptList> lqw = Wrappers.lambdaQuery();
                    lqw.eq(bo.getId() != null, DhumanPptList::getId, bo.getId());
                    lqw.eq(StringUtils.isNotBlank(bo.getTenantId()), DhumanPptList::getTenantId, bo.getTenantId());
                    lqw.eq(bo.getDhumanPptId() != null, DhumanPptList::getDhumanPptId, bo.getDhumanPptId());
                    lqw.eq(StringUtils.isNotBlank(bo.getImageName()), DhumanPptList::getImageName, bo.getImageName());
                    lqw.eq(StringUtils.isNotBlank(bo.getImageUrl()), DhumanPptList::getImageUrl, bo.getImageUrl());
                    lqw.eq(bo.getImageSort() != null, DhumanPptList::getImageSort, bo.getImageSort());
                    lqw.eq(StringUtils.isNotBlank(bo.getCopyText()), DhumanPptList::getCopyText, bo.getCopyText());
                    lqw.eq(StringUtils.isNotBlank(bo.getTtsExpression()), DhumanPptList::getTtsExpression, bo.getTtsExpression());
                    lqw.eq(StringUtils.isNotBlank(bo.getTtsStatus()), DhumanPptList::getTtsStatus, bo.getTtsStatus());
                    lqw.eq(StringUtils.isNotBlank(bo.getTtsResponse()), DhumanPptList::getTtsResponse, bo.getTtsResponse());
                    lqw.eq(StringUtils.isNotBlank(bo.getTtsAudioUrl()), DhumanPptList::getTtsAudioUrl, bo.getTtsAudioUrl());
                    lqw.eq(StringUtils.isNotBlank(bo.getTtsSrtUrl()), DhumanPptList::getTtsSrtUrl, bo.getTtsSrtUrl());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanExpression()), DhumanPptList::getDhumanExpression, bo.getDhumanExpression());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanStatus()), DhumanPptList::getDhumanStatus, bo.getDhumanStatus());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanResponse()), DhumanPptList::getDhumanResponse, bo.getDhumanResponse());
		            lqw.eq(StringUtils.isNotBlank(bo.getDhumanVideoId()), DhumanPptList::getDhumanVideoId, bo.getDhumanVideoId());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanVideoUrl()), DhumanPptList::getDhumanVideoUrl, bo.getDhumanVideoUrl());
                    lqw.eq(StringUtils.isNotBlank(bo.getDhumanVideoMaskUrl()), DhumanPptList::getDhumanVideoMaskUrl, bo.getDhumanVideoMaskUrl());
                    lqw.eq(bo.getCreateBy() != null, DhumanPptList::getCreateBy, bo.getCreateBy());
                    lqw.eq(bo.getCreateTime() != null, DhumanPptList::getCreateTime, bo.getCreateTime());
                    lqw.eq(bo.getUpdateBy() != null, DhumanPptList::getUpdateBy, bo.getUpdateBy());
                    lqw.eq(bo.getUpdateTime() != null, DhumanPptList::getUpdateTime, bo.getUpdateTime());
                    lqw.eq(StringUtils.isNotBlank(bo.getRemark()), DhumanPptList::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增数字人讲PPT子表
     */
    @Override
    public Boolean insertByBo(DhumanPptListBo bo) {
        DhumanPptList add = MapstructUtils.convert(bo, DhumanPptList. class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数字人讲PPT子表
     */
    @Override
    public Boolean updateByBo(DhumanPptListBo bo) {
        DhumanPptList update = MapstructUtils.convert(bo, DhumanPptList. class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DhumanPptList entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数字人讲PPT子表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
